package com.asset.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class QRExcelUtil {
	
@SuppressWarnings("deprecation")
public static void exportTable(List<BufferedImage> images){
	
	FileOutputStream fileOut = null;
	try {
		// 创建一个工作薄
		@SuppressWarnings("resource")
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet1 = wb.createSheet("new sheet");
		HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
		
		short i = 0;
		int count = 0;
		for (BufferedImage image : images) {
			ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
			ImageIO.write(image, "jpg", byteArrayOut);
			
			if(i == 1){
				HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) (0+i*3),0+6*count , (short) (2+i*3), 5+6*count);
				anchor.setAnchorType(0);
				// 插入图片
				patriarch.createPicture(anchor,wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
				i = 0;
				count ++;
			}else{
				HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) (0+i*3),0+6*count, (short) (2+i*3), 5+6*count);
				anchor.setAnchorType(0);
				// 插入图片
				patriarch.createPicture(anchor,wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
				i+=1;
			}
			
			
		}
		HSSFRow row = sheet1.createRow(10);
		short s = 10;
		HSSFCell cell = row.createCell(s);
		HSSFCellStyle style = wb.createCellStyle();
		HSSFFont font = wb.createFont();
		font.setStrikeout(true);
		style.setFont(font);
		cell.setCellStyle(style);
		fileOut = new FileOutputStream("C:/Users/Administrator/Desktop/work.xls");
		// 写入excel文件
		wb.write(fileOut);
		fileOut.close();
	} catch (IOException io) {
		io.printStackTrace();
		System.out.println("io erorr : " + io.getMessage());
	} finally {
		if (fileOut != null) {
			try {
				fileOut.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
	public static void main(String[] args) {
		List<BufferedImage> images = new ArrayList<>();
		try {
			images.add(ImageIO.read(new File("C:/Users/Administrator/Desktop/chinac2.jpg")));
			images.add(ImageIO.read(new File("C:/Users/Administrator/Desktop/chinac3.jpg")));
			images.add(ImageIO.read(new File("C:/Users/Administrator/Desktop/chinac4.jpg")));
			images.add(ImageIO.read(new File("C:/Users/Administrator/Desktop/chinac5.jpg")));
			images.add(ImageIO.read(new File("C:/Users/Administrator/Desktop/chinac6.jpg")));
			images.add(ImageIO.read(new File("C:/Users/Administrator/Desktop/chinac7.jpg")));
		} catch (IOException e) {
			e.printStackTrace();
		}
		exportTable(images);
	}
}
